<?php

namespace builder\test\controllers;

use builder\base\BaseController;
use builder\helper\H5;
use builder\tree_table\TreeTableBuilder;
use common\models\Region;

/**
 * Tree-Table Builder Example
 */
class TreeTableController extends BaseController
{
    public $publicActions = ['index', 'children', 'demo'];

    public function actionDemo()
    {
        if ($this->isAjax) {
            return $this->asOk('Success');
        }

        return 'Demo';
    }

    /**
     * demo
     * @return string
     * @throws \Throwable
     * @throws \builder\base\InvalidInstanceException
     * @throws \builder\base\NotFoundParamsException
     */
    public function actionIndex()
    {
        $viewBuilder = $this->viewBuilder;

        $treeTable = $viewBuilder->treeTable;
        $treeTable->setId('TreeTable_Index');
        if ($this->isAjax) { // AJAX
            $treeTable->setQuery(function () {
                $params = $this->filteredGet;
                $query = Region::query()->where([
                    'pid' => 0,
                ]);

                $query->filterWhere([
                    'like', 'name', isset($params['keyword']) ? $params['keyword'] : null,
                ]);

                return $query;
            })
                ->setOrderBy(['id' => SORT_ASC])
                ->setTtColumns([
                    'id',
                    'name',
                    'pid',
                    'isParent' => function ($row) {
                        return 1;
                    },
                    'ro_delete3' => function ($row) {
                        return $row['id'] % 2;
                    },
                ])->setExtraData(function (TreeTableBuilder $treeTableBuilder) {
                    return [
                        'tol' => $treeTableBuilder->query->count(),
                        'a' => 100000,
                    ];
                })->setTotalRow([
                    'id' => '共：1000000',
                    'pid' => '这是PID后端统计。'
                ]);

            $render = $viewBuilder->render();
            //return $treeTable->data;
            return $render;

        } else { // HTML
            $treeTable->setTitle('管理员列表')
                ->setExtraDataTpl([
                    'tol' => '当前总数量',
                    'a' => '所在院校',
                ])
                ->setAutoSort(false)
                ->setOpenTotalRow()
                ->registerPointcut(H5::alert('测试一下', 'warning'))
                ->registerPointcut(H5::alert('测试一下'), TreeTableBuilder::POS_END)
                ->setAjaxRoute('/ymtest/tree-table/index')
                ->setTreeTableHead([
                    $this->treeTableHead->checkbox(),
                    $this->treeTableHead->field('id')->title('ID')->minWidth(250)->sort(),
                    $this->treeTableHead->field('name')->title('账号')->minWidth(250),
                    $this->treeTableHead->field('pid')->title('PID')->minWidth(250),
                    $this->treeTableHead->toolbar()->title('操作项')->width(150),
                ])
                ->setTreeAsyncOption('/ymtest/tree-table/children')
                ->setTreeCustomNameOption([])
                ->setTreeViewOption([])
                ->setTreeDataOption([])
                ->setTtRowOperation([
                    // 详情
                    $this->ttRowOperation->modal()->title('详情')->icon('bi bi-plus-lg f15')->route('ymtest/tree-table/demo')->closeBtn()->titleCloseBtnHidden(),
                    $this->ttRowOperation->ajax()->title('删除')->icon('bi bi-x-lg')->route('ymtest/tree-table/demo')->method('POST')->group(),
                    $this->ttRowOperation->ajax()->title('我要删除1')->icon('bi bi-x-lg')->route('ymtest/tree-table/demo')->method('POST')->group(),
                    $this->ttRowOperation->ajax()->title('我要删除2')->icon('bi bi-x-lg')->route('ymtest/tree-table/demo')->method('POST')->group(),
                    $this->ttRowOperation->ajax()->title('我要删除3')->id('ro_delete3')->icon('bi bi-x-lg')->route('ymtest/tree-table/demo')->method('POST')->group(),
                ])
                ->setTtToolbarExpandAll()
                ->setTtToolbarCustom([
                    // 表单
                    $this->ttToolbarCustom->modal()->btnClass('layui-btn-primary layui-border')->title('表单')->icon('bi bi-plus-lg f15')
                        ->route('ymtest/tree-table/demo')->urlAttachFilter()->titleMaxminBtnDisplay(),
                    // 列表
                    $this->ttToolbarCustom->modal()->btnClass('layui-btn-primary layui-border-green')->title('列表')->params(['id'])->icon('bi bi-plus-lg f15')->route('ymtest/tree-table/demo')->closeBtn(),
                    // 选项卡
                    $this->ttToolbarCustom->modal()->btnClass('layui-btn-primary layui-border-blue')->title('选项卡')->icon('bi bi-pencil-square f13')->route('ymtest/tree-table/demo')->closeBtn(),
                    // AJAX
                    $this->ttToolbarCustom->page()->btnClass('layui-btn-primary layui-border-orange')->title('AJAX')->icon('bi bi-lock')->targetBlank()->params(['id'])->route('ymtest/tree-table/demo'),
                ])
                ->setTtToolbarFilter([
                    'keyword' => $this->ttToolbarFilter->text()->label('关键词')->placeholder('请输入账号/姓名')->defaultValue()->attribute(['test' => 1]),
                    //'username' => $this->ttToolbarFilter->datetime()->style(['width' => '800px'])->label('注册时间')->placeholder('请选择注册时间'),
                    //'username' => $this->ttToolbarFilter->datetime()->label('注册时间')->placeholder('请选择注册时间'),
                    'status' => $this->ttToolbarFilter->select()->label('用户状态')->placeholder('请选择用户状态')->options([
                        '测试' => [
                            'normal' => '正常',
                            'deny' => '禁用',
                        ],
                        '测试2' => [
                            'normal1' => '正常1',
                            'deny1' => '禁用1',
                        ],
                    ])->disabled(['normal']),
                    'h' => $this->ttToolbarFilter->selectLayui()->label('通天塔')->options([
                        ['value' => '孙悟空', 'label' => '孙悟空', 'disabled' => false],
                        ['value' => '猪八戒', 'label' => '猪八戒', 'disabled' => false],
                        ['value' => '沙和尚', 'label' => '沙和尚', 'disabled' => false],
                        ['value' => '唐生', 'label' => '唐生', 'disabled' => false],
                        ['value' => '李静', 'label' => '李静', 'disabled' => false],
                        ['value' => '哪吒', 'label' => '哪吒', 'disabled' => false],
                        ['value' => '木吒', 'label' => '木吒', 'disabled' => false],
                        ['value' => '二郎神', 'label' => '二郎神', 'disabled' => true],
                        ['value' => '哮天犬', 'label' => '哮天犬', 'disabled' => false],
                        ['value' => '太白金星', 'label' => '太白金星', 'disabled' => false],
                    ])->defaultValue('猪八戒')->attribute(['data-attr' => 1])->placeholder('请选择通天塔')->uiClass('w-300')->style(['font-size' => '15px'])
                        ->disabled(false)->search_fuzzy(true)->search(true)->search_caseSensitive(true)->creatable(true)->help('这只是一个简单的帮助！'),
                ])
                ->setAutoRequest(true);

            $viewBuilder->setPartial(!!@$this->get['_partial']);
            return $viewBuilder->render();
        }
    }

    public function actionChildren()
    {
        $data = Region::query([
            'id',
            'name',
            'pid',
        ])->where(['pid' => $this->get['parentId']])->all();
        foreach ($data as &$item) {
            $item['isParent'] = 1;
        }

        return $this->asOk('success', [
            'count' => count($data),
            'list' => $data,
        ]);
    }
}